<?php

App::uses('AppModel', 'Model');

/**
 * Province Model
 *
 * @property Amphur $Amphur
 */
class Province extends AppModel {

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'code' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'name_tha' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'name_eng' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'Amphur' => array(
            'className' => 'Amphur',
            'foreignKey' => 'province_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    public function listOption() {
        $fieldPro = 'name_' . CakeSession::read('Config.language');
        $data = $this->Find('all', array(
            'fields' => array("Amphur.id", "concat(Province." . $fieldPro . ",', ',Amphur." . $fieldPro . ") as name"),
            'joins' => array(
                array(
                    'table' => 'amphurs',
                    'alias' => 'Amphur',
                    'conditions' => array(
                        'Province.id = Amphur.province_id'
                    )
                )
            ),
            'conditions' => array(
                'Province.id = Amphur.province_id',
                'Province.id'=>array(1,38)
            ),
            'order' => array('Amphur.province_id desc','Amphur.id asc'),
            'recursive' => -1
        ));
        $new_data = array();
        foreach ($data as $a):
            $new_data[$a['Amphur']['id']]= $a[0]['name'];
            //array_push($new_data,array($a['Amphur']['id']=>$a[0]['name']));
        endforeach;

        return $new_data;
    }

}
